package com.cloud.school.controller;

import com.cloud.school.client.SchoolRemoteClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @author 24270
 */
@RestController
public class SchoolController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SchoolRemoteClient remoteClient;

    /**
     * Ribbon调用client2的showInfo方法
     * @return String 学校名称-姓名
     */
    @GetMapping("/ribbon/showInfo")
    @HystrixCommand(fallbackMethod = "failBackOne")
    public String showInfo() {
        return restTemplate.getForObject("http://client2/showInfo", String.class);
    }

    @GetMapping("/feign/showInfo")
    @HystrixCommand(fallbackMethod = "failBackTwo")
    public String showInfoByFeign() {
        return remoteClient.showInfoByFeign();
    }

    /**
     * 对应上面的方法，参数必须一致，当访问失败时，hystrix直接回调用此方法
     * @return 失败调用时，返回默认值
     */
    public String failBackOne() {
        return "目前网络忙，请稍后再试！";
    }

    public String failBackTwo() {
        return "当前时间： " + new Date().toString() +  "  --请联系网络管理员";
    }

}
